const express=require('express')
const path=require('path')
const fs=require('fs')
let app=express()
app.listen(3000,()=>{
    console.log('3000ok');
})

app.get("/register",(req,res)=>{
    res.sendFile(path.join(__dirname,"./login.html"))
})


// 引入 formidable
let formidable=require("formidable")


app.post("/register",(req,res)=>{

    // 设置文件临时上传的目录
    let  form=formidable({
        uploadDir: path.join(__dirname,"./tempdir")
    })

    // 解析信息
    form.parse(req,(err,fields,files)=>{
        
        // 错误拦截
        if(err){
            res.send("文件上传失败")
            return
        }

        // 剪切：
        // fs.renameSync(旧路径,新路径)

        //使用原来的名字
        let oldpath=files.touxiang.filepath
        
        // let newpath=path.join(__dirname,"./uploads", files.touxiang.originalFilename    )

        let ext= path.extname(files.touxiang.originalFilename)
        let newpath=path.join(__dirname,"./uploads", files.touxiang.newFilename)+ext

        fs.renameSync(oldpath,newpath)


        res.send("文件上传成功")

    })

})

/* {
    "touxiang": {
        "size": 13113,
        "filepath": "C:\\Users\\Administrator\\Desktop\\软件学院20220411\\day13express中间件\\代码\\03第三方中间件\\tempdir\\f0edffb1869dbff6231fc4500",
        "newFilename": "f0edffb1869dbff6231fc4500",
        "mimetype": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        "mtime": "2022-08-11T08:04:32.306Z",
        "originalFilename": "Web各阶段考核标准.docx"
    }
} */